package com.hands.transfercenter.service.impl;

import com.hands.commen.entity.commen.DomainJudgment;
import com.hands.commen.entity.commen.SearchInfo;
import com.hands.commen.entity.wx.Base64Util;
import com.hands.commen.entity.wx.MsgEntity;
import com.hands.commen.entity.wx.MsgHeader;
import com.hands.commen.utils.GZipHelper;
import com.hands.commen.utils.HttpClientHelper;
import com.hands.commen.utils.RandomUtils;
import com.hands.service.VoiceRpcService;
import com.hands.service.WxxRpcService;
import com.hands.transfercenter.VoiceInfoService;
import com.hands.transfercenter.baiduApi.ApiNlpDataHelper;
import com.hands.transfercenter.config.uriConfig;
import com.hands.transfercenter.mapper.DomainJudgmentMapper;
import com.hands.transfercenter.mapper.SearchInfoMapper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.apache.dubbo.rpc.RpcContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

//@Service
@Service(interfaceClass = VoiceInfoService.class, version = "1.0.1", timeout = 15000)
public class VoiceServiceImpl implements VoiceInfoService {

    private static final Logger logger = LoggerFactory.getLogger(VoiceServiceImpl.class);

    @Autowired
    private DomainJudgmentMapper domainJudgmentMapper;


    //RPC调用
//    @Reference()
//    private VoiceRpcService voiceRpcService;


    @Reference(interfaceClass = WxxRpcService.class, version = "1.0.1", async = true,check = false,timeout = 15000)
    private WxxRpcService  wxxRpcService;

    @Override
    public String VoiceInfo(String massage, String voiceId) throws InterruptedException, ExecutionException {


        //1，调用百度分词，匹配出相应的领域




        //调用百度分词接口进行词语解析
        logger.info("分词的数据{}",massage);
        String words = ApiNlpDataHelper.getWords(massage, "pos");
        logger.info("分词信息{}",words);
        net.sf.json.JSONArray arr = JSONArray.fromObject(words);
        System.out.println(arr.get(0));
        //        JSONArray jsonArray= JSONArray.parseArray(words);
//        for (int i=0;i<jsonArray.size();i++){
//            String msg = jsonArray.get(i).toString();
//        }
        //

        //2，根据不同的领域进行相应的处理
        //下发边缘网关命令
        String field="1";
        String reponseMsg=null;
        if(field !=null){
            reponseMsg=instructionsGateway(field,voiceId);
        }




        //根据传过来的消息查询数据库结果
        List<DomainJudgment> domainJudgments = domainJudgmentMapper.selectDomainJudgmentList(new DomainJudgment());
        //System.out.println("查询结果=="+domainJudgments);
        //模拟下发到deviceopen领域



        return reponseMsg;
    }

    //下发边缘网关命令
    private String instructionsGateway(String field, String voiceId) throws InterruptedException, ExecutionException {

        String soundHostData = HttpClientHelper.doGet(String.format(uriConfig.soundHostUrl, voiceId));//请求音箱设备信息地址

        JSONObject jo = JSONObject.fromObject(soundHostData);





        System.out.println(jo);
        String msgid="#http"+ RandomUtils.getShortUuid();
        MsgEntity msg = new MsgEntity();

        msg.setHeader(new MsgHeader());
        msg.getHeader().setOs(4);
        msg.getHeader().setMsgId(msgid);
        msg.getHeader().setMac(msgid);
        msg.getHeader().setBizCode(815);
        ///根据房间号，别名获取主板mac地址
        msg.getHeader().setToUserId("001B101D0ED6");
        String  body ="{\"pageNo\":"+1+",\"pageSize\":"+10+",\"roomId\":"+1+"}";
        if (body.length()>0) {
            body = Base64Util.encode(GZipHelper.compress(body, "UTF-8"));
            msg.setBody(body);
        }
        String  reponseMsg=wxxRpcService.InstructionsInfo(msg);
        Future<String> future = RpcContext.getContext().getFuture();
        logger.info("future的值为{},直接获取的数据{}",future,reponseMsg);
        String futureInfo = future.get();

        if(future !=null){
            logger.info("返回来的值{}",futureInfo);
            while (true){
                if(futureInfo !=null ){
                    reponseMsg =futureInfo;
                    break;
                }else{
                    futureInfo = future.get();
                }

            }
        }
        logger.info("reponseMsg={}",reponseMsg);
      return reponseMsg;
    }
}
